Notes on Maintainable JavaScript
Part one covers coding style to make team code look as if written by a single person. Part two covers programming practices, with many valuable JavaScript programming lessons. Part three is about automation, but the toolchain it introduces feels quite outdated by the end of 2016.
Notes on Maintainable JavaScript
Part One
Basic Formatting
- It is recommended to add a blank line before each flow control statement (such as
ifandforstatements). - Variable names should be prefixed with nouns, while function names should be prefixed with verbs. Common verb conventions:
| Verb | Meaning |
|---|---|
| can | Returns a boolean |
| has | Returns a boolean |
| is | Returns a boolean |
| get | Returns a non-boolean value |
| set | Used to save a value |
Constants should be written in uppercase letters with words separated by underscores.
Scenarios for using
null(think of it as an object placeholder):a. Initializing a variable that may later be assigned an object
b. Comparing with an already initialized variable (which may or may not be an object)
c. Passing as an argument when a function expects an object
d. Returning as a value when a function’s return value is expected to be an object
null == undefined // true
typeof null === object // true
Leave a blank line before comments, except at the beginning of a file.
Code that is counterintuitive or intentionally unconventional should be commented.
Leave a blank line after declarations.
Primitive wrapper types: String, Boolean, Number — primitive values themselves do not have object characteristics.
Part Two
- Use a single global variable; mount all variables under one specific global variable.
- Create objects on the first level of the global object to serve as namespaces.
- Use function wrappers to create a zero-global-variable scenario:
1 | (function(win) { |
- Separate application logic — for example, encapsulate event handlers in a global object:
1 | const MyApp = { |
- Do not distribute the event object; only pass the required parameters:
1 | const MyApp = { |
- Make event handlers the only functions that touch the
eventobject. Following points 4, 5, and 6 makes code easier to test:
1 | const MyApp = { |
- When an object from frame A is passed into frame B, each frame effectively has its own copy, resulting in:
1 | // true |
- Use
Array.isArrayto check for arrays. - To check whether an object has a property (avoid falsy property values — using
if(obj['count'])is not recommended), use theinoperator or thehasOwnProperty()method. - Extract configuration data (hardcoded values).
- Throwing errors is like leaving yourself a sticky note explaining what went wrong.
- The only difference between the wrapper pattern and the adapter pattern is that the former creates a new interface while the latter implements an existing one.
- Avoid using feature inference and browser inference; use feature detection and user agent detection judiciously.
Notes on Maintainable JavaScript
http://quanru.github.io/2016/11/27/Notes on Maintainable JavaScript

